package com.hjh.gameforum.aop;

import com.hjh.gameforum.dto.LoginDto;
import com.hjh.gameforum.dto.RegisterDto;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Slf4j
@Component
public class MyAop {

    @Around("execution(* com.hjh.gameforum.controller.*.*(..))")
    public Object timeStatistics(ProceedingJoinPoint pjp) throws Throwable {  // 修改返回类型和异常声明
        long start = System.currentTimeMillis();
        try {
            Object result = pjp.proceed();  // 捕获返回值
            long end = System.currentTimeMillis();
            log.info("{} {} {} 运行时间：{}ms",
                    pjp.getTarget().getClass().getName(),
                    pjp.getSignature().getName(),
                    pjp.getArgs(),
                    end - start);
            return result;  // 返回结果
        } catch (Throwable throwable) {
            long end = System.currentTimeMillis();
            log.error("方法执行出错: {} {} {} 运行时间：{}ms",
                    pjp.getTarget().getClass().getName(),
                    pjp.getSignature().getName(),
                    pjp.getArgs(),
                    end - start);
            throw throwable;  // 重新抛出异常
        }
    }

    @Around("execution(* com.hjh.gameforum.controller.UserController.login(..)) || " +
            "execution(* com.hjh.gameforum.controller.UserController.register(..))")
    public Object checkSpace(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();

        // 1. 参数校验
        if (args == null || args.length == 0 || args[0] == null) {
            return pjp.proceed();
        }
        log.debug("原始参数: {}", args[0]);

        // 2. 统一处理DTO
        Object dto = args[0];
        if (dto instanceof LoginDto) {
            LoginDto loginDto = (LoginDto) dto;
            loginDto.setUsername(processString(loginDto.getUsername()));
            loginDto.setPassword(processString(loginDto.getPassword(), false)); // 密码不处理中间空格
        }
        else if (dto instanceof RegisterDto) {
            RegisterDto registerDto = (RegisterDto) dto;
            registerDto.setUsername(processString(registerDto.getUsername()));
            registerDto.setEmail(processString(registerDto.getEmail()));
            registerDto.setPassword(processString(registerDto.getPassword(), false));
            registerDto.setConfirmPassword(processString(registerDto.getConfirmPassword(), false));
        }

        // 3. 添加日志记录
        log.debug("处理后的参数: {}", dto);

        return pjp.proceed(args);
    }

    // 字符串处理工具方法
    private String processString(String input) {
        return processString(input, true);
    }

    private String processString(String input, boolean removeMiddleSpace) {
        if (input == null) {
            return null;
        }
        String result = input.trim();
        return removeMiddleSpace ? result.replaceAll("\\s+", "") : result;
    }
}
